package com.ld.hpfs.web.controller;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ld.hpfs.common.jackson.JackJson;
import com.ld.hpfs.pojo.ExtPager;
import com.ld.hpfs.service.ErrorDetailService;

@Controller
public class ErrorDetailController {
	private static final Logger logger = LoggerFactory.getLogger(ErrorDetailController.class);

	@Autowired
	private ErrorDetailService errorDetailService;
	
	@RequestMapping(value = "/getErrorDetailList")
	@ResponseBody
	public void getImpSchemeList(ExtPager page,HttpSession session, HttpServletRequest request,HttpServletResponse response,PrintWriter writer){
		String importCode = (String)request.getParameter("importCode") == null ? null : (String)request.getParameter("importCode");
		HashMap<String,Object> param = new HashMap<String,Object>();
		HashMap<String,Object> result = new HashMap<String,Object>();
		response.setContentType("text/plain;charset=UTF-8");
		try {
			ArrayList<Map<String,String>> field = page.getSortField();
			StringBuffer sort = new StringBuffer("");	
				param.put("pageEnd",page.getStart() + page.getLimit());
				if(field!=null && field.size()>0){
					for(Map<String, String> map : field){
			        	sort.append(map.get("property") + " " + map.get("direction") + ",");
			        }
					param.put("orderBy", sort.substring(0,sort.length()-1));
				} else  {
					param.put("orderBy", "ERROR_HID");
				}				
				param.put("pageStart", page.getStart());	
				param.put("importCode", importCode);
			ArrayList<HashMap<String,Object>> logs = errorDetailService.getErrorDetailList(param);
			if(logs!=null&&logs.size()>0){
				result.put("success", true);
				result.put("total", param.get("pageCount"));
				result.put("data", logs);
			} else {
				result.put("success", true);
				result.put("total", param.get("pageCount"));
				result.put("data", "");
			}
			String returnMsg = JackJson.fromObjectToJson(result);
			logger.debug("{}", returnMsg);
			writer.print(returnMsg);		
		} catch (Exception e) {
			logger.error("Exception: ", e);
		} finally {
			writer.flush();
			writer.close();
		}
	}
}
